package com.axend.aerosense.common.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;

import com.axend.aerosense.common.R;
import com.axend.aerosense.common.bean.NoticeBean;
import com.axend.aerosense.common.databinding.CommonNoticeDialogBinding;


public class NoticeDialog extends Dialog {

    private CommonNoticeDialogBinding databinding;
    private NoticeBean noticeBean;
    private String message;

    public NoticeDialog(Context context, NoticeBean noticeBean) {
        super(context, R.style.Common_CustomDialog);
        this.noticeBean = noticeBean;
        databinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.common_notice_dialog, null, false);
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //设置提示框位置
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
    }

    public NoticeDialog(Context context, String msg) {
        super(context, R.style.Common_CustomDialog);
        this.message = msg;
        databinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.common_notice_dialog, null, false);
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //设置提示框位置
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
        onClickBottomListener = new OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                dismiss();
            }

            @Override
            public void onCloseClick() {
                dismiss();
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(databinding.getRoot());
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        init();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void init() {
        //设置更新按钮被点击后，向外界提供监听
        databinding.commonBtnNow.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onPositiveClick();
            }
        });

        databinding.commonClose.setOnClickListener(v->{
            if (onClickBottomListener != null) {
                onClickBottomListener.onCloseClick();
            }
        });

        if (null != noticeBean && !TextUtils.isEmpty(noticeBean.noticeMsg)){
            databinding.commonNotice.setText(noticeBean.noticeMsg);
        }else {
            if (null != message && !TextUtils.isEmpty(message)){
                databinding.commonNotice.setText(message);
            }
        }
    }



    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;

    public void setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
    }

    public interface OnClickBottomListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();
        /**
         * 关闭按钮点击事件
         */
        public void onCloseClick();
    }

}
